package com.test.conf;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class SessionFactory {
	/**
	 * 1.从配置文件中获取sqlSessionFactroy  全局配置文件
	 * 2.得到sqlSession 对象   (他可以执行已经在mybatis-config.xml 中映射好的sql语句)
	 *
	 * huyong
	 * @return
	 * @throws IOException
	 * Jul 21, 2019
	 */
	public SqlSession createSqlSession() throws IOException {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		Properties properties=new Properties();
		BufferedReader bufferedReader = new BufferedReader(new FileReader(System.getProperty("user.dir")+"/conf/db.properties"));
		properties.load(bufferedReader);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream,properties);
		return sqlSessionFactory.openSession();
	}
	
	public SqlSessionFactory sqlSessionFactory() throws IOException {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		Properties properties=new Properties();
		BufferedReader bufferedReader = new BufferedReader(new FileReader(System.getProperty("user.dir")+"/conf/db.properties"));
		properties.load(bufferedReader);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream,properties);
		return sqlSessionFactory;
	}

	
}
